import 'reflect-metadata'
function Injectable(target){} // 必须要加这个函数才能取到Reflect.getMetadata

@Injectable
class Engine{
    start(){
        console.log('Engine started')
    }
}
@Injectable
class Car{

   constructor(private engine: Engine){

   }
   drive(){
    this.engine.start()
    console.log('Car is runing')
   }
}
// 定义依赖注入容器
class DIContainer{
    private services = new Map()
    register(name, Service){
        this.services.set(name, Service)
    }
    resolve(name){
        const Service = this.services.get(name)
        const dependencies = Reflect.getMetadata('design:paramtypes', Service) || []
        const injections = dependencies.map(dependency => this.resolve(dependency.name))
        return new Service(...injections)
    }
}

const container = new DIContainer()
container.register('Engine', Engine)
container.register('Car', Car)

const car = container.resolve('Car')
car.drive()